var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var valide = libs.formValide;
var VTabs = require('modules/list/tabs_list');
var _ = libs.lodash
var _user = SA.get('USER')

//当前页公共变量
var _page = {}
var _gotonext = 'uc/edit'
var rdata = {};

var menus = [
    // {id: '111', key: "abc", title: "苹果", 'data-api': "/xxx?id=111"},
    // {id: '111', key: "abc", title: "苹果"},
    // {id: '112', key: "bcd", title: "三星"},
    // {id: '113', key: "cde", title: "华为"},
    // {id: '114', key: "rrr", title: "小米"}
]

var lists = [
    // {id: '111', key: "abc", title: "苹果的手机", url: "http://www.163.com"},
    // {id: '112', key: "bcd", title: "三星的手机", url: "http://www.163.com"},
    // {id: '113', key: "cde", title: "华为的手机", url: "http://www.163.com"},
    // {id: '114', key: "rrr", title: "小米的手机", url: "http://www.163.com"},
    // {id: '111', key: "abc", title: "苹果的手机", url: "http://www.163.com"},
    // {id: '112', key: "bcd", title: "三星的手机", url: "http://www.163.com"},
    // {id: '113', key: "cde", title: "华为的手机", url: "http://www.163.com"},
    // {id: '114', key: "rrr", title: "小米的手机", url: "http://www.163.com"},
    // {id: '111', key: "abc", title: "苹果的手机", url: "http://www.163.com"},
    // {id: '112', key: "bcd", title: "三星的手机", url: "http://www.163.com"},
    // {id: '113', key: "cde", title: "华为的手机", url: "http://www.163.com"},
    // {id: '114', key: "rrr", title: "小米的手机", url: "http://www.163.com"},
    // {id: '111', key: "abc", title: "苹果的手机", url: "http://www.163.com"},
    // {id: '112', key: "bcd", title: "三星的手机", url: "http://www.163.com"},
    // {id: '113', key: "cde", title: "华为的手机", url: "http://www.163.com"},
    // {id: '114', key: "rrr", title: "小米的手机", url: "http://www.163.com"},
    // {id: '111', key: "abc", title: "苹果的手机", url: "http://www.163.com"},
    // {id: '112', key: "bcd", title: "三星的手机", url: "http://www.163.com"},
    // {id: '113', key: "cde", title: "华为的手机", url: "http://www.163.com"},
    // {id: '114', key: "rrr", title: "小米的手机", url: "http://www.163.com"}
]

// 更新所有市列表
function deal_menulist_data(data,cId){
    menus = []
    if (_.isArray(data)){
        data.map(function(item, i){
            if (item.cityId===cId){
                rdata.cityId = item.cityId
                rdata.cityName = item.cityName
            }
            menus.push({
                id: item.cityId,
                title: item.cityName,
                className: (function(){ return item.cityId=== cId ? 'item active' : '' })(),
                key: '',
                "data-api": '/getCommunity'
            })
        })
    }
}


// 更新某市的小区列表
// {
// address : "",
// cityId : "",
// cityName : "",
// communityId : "01befacdd3194d44b95f52df650e745f",
// communityName : "aaaaaa",
// districtId : "450102",
// districtName : "",
// fullAddress : "",
// provinceId : "",
// provinceName : ""
// }
function deal_list_data(data,cod){
    console.log(cod);
    lists = []
    if (_.isArray(data)){
        data.map(function(item){
            lists.push({
                id: item.communityId,
                title: item.communityName,
                'data-dscd': item.districtId,
                'data-dscdname': item.districtName,
                className: (function(){ return item.communityId=== cod ? 'item glxq' : '' })()
            })
        })
        SA.setter('LocationLists', {data: lists})
    }
}

var menu_funs = {
    item: function(){
        $(this).click(function(){
            $(this).siblings().removeClass('active')
            $(this).addClass('active')
            var _api = $(this).attr('data-api')
            var _cid = $(this).attr('data-id')
            rdata.cityId = _cid
            rdata.cityName = $(this).text()
            // ?cityId='+item.cityId
            api.req('/getCommunity', {cityId: _cid, method: 'get'}, function(data){
                deal_list_data(data)
            })
        })
    }
}

var list_funs = {
    item: function(){
        $(this).click(function(){
            var _api = $(this).attr('data-api')
            var _cid = $(this).attr('data-id')
            var _dscd = $(this).attr('data-dscd')
            var _dscdname = $(this).attr('data-dscdname')
            var _title = $(this).text()
            var postdata = {
                login: _user.login,
                currentCommunityId: _cid,
                method: 'get'
            }

            var _rdata = {
                _location: true,
                communityId: _cid,
                communityName: _title,
                dscd: _dscd,
                districtName: _dscdname
            }

            rdata = _.assign(rdata, _rdata)

            if (_gotonext==='uc/addressedit'){
                // SA.deleter('Uuinfo')
                router.goback(rdata)
            }
            else {
                api.req('/weixinSwitchCommunity', postdata, function(data){
                    if (!data.code){
                        var _reurl = _user.reurl;
                        api.req('/userInfo', {login: _user.login, method: 'get'}, function(ddd){
                            SA.set('USER', {
                                login: _user.login,
                                info: ddd
                            })
                            // if (_gotonext==='uc/addressedit'){
                            //     // SA.deleter('Uuinfo')
                            //     router.goback()
                            // }
                            // else
                            if (_reurl){
                                router(_reurl)
                            }
                            else{
                                // router(_gotonext)
                                var _pre = router.pre();
                                if (_pre.hash==='uc/autolocation'){
                                    router(_gotonext)
                                }
                                else{
                                    router.goback()
                                }
                            }

                        })
                    }
                })
            }

        })
    },
    scrollEnd: function(_name, _next){

    }
}

var Location = React.createClass({
    mixins: [ItemMixin],
    render: function(){
        return(
            <div id="location" className="location">
                {/*<div className="defalut_search">
                 <input className="defalut_search_input box-shadow" type="text" placeholder="搜索关链字"/>
                 <div className="defalut_search_btn"></div>
                 </div>*/}
                <div id='location_tab' style={{height:"100%"}}></div>
            </div>
        )
    }
})
function bindEventLocation(){
    o_user = SA.get('USER');
    api.req('/userInfo', {login: o_user.login, method: 'get'}, function(dataC){
        if(dataC.currentCommunity == null || dataC.currentCommunity == "undefined"){
            var cId = "110000";
            var cod = "";
        }else{
            var cId = dataC.currentCommunity.cityId;
            var cod = dataC.currentCommunity.communityId;
        }
        api.req('/getAllCity', function(data){
            deal_menulist_data(data,cId)
            api.req('/getCommunity', {cityId: cId, method: 'get'}, function(data){
                console.log(data);
                deal_list_data(data,cod)
                VTabs(menus, lists, {
                    container: 'location_tab',
                    globalName: {
                        menu: 'LocationMenus',
                        list: 'LocationLists'
                    },
                    itemMethod: {
                        menu: menu_funs.item,
                        list: list_funs.item
                    },
                    scrollEnd: {
                        list: list_funs.scrollEnd
                    },
                    empty: {
                        menu: <div></div>,
                        list: <div></div>
                    }
                })
            })
        })
    })
}

function start(name){
    return pages.new({
        goback: function(){
            _user = SA.get('USER')
            var _pre = router.pre()
            if (_pre && _pre.hash=== 'uc/autolocation'){
                router.cb = function(){
                    if (wx){
                        wx.closeWindow()
                    }
                }
            }
            else {
                if (!_user.info.currentCommunity){
                    router.cb = function(){
                        if (wx){
                            wx.closeWindow()
                        }
                    }
                }
                else{
                    if (_pre && _pre.hash!=='uc/index'){
                        router.cb = function(){
                            router.goback()
                        }
                    }
                    else{
                        router.cb = function(){
                            router('uc/index')
                        }
                    }
                }
            }
        },
        trigger:function(self, data){
            _user = SA.get('USER')
            if (data.back && typeof data.back==='string'){
                _gotonext = data.back
            }
            this.libs.changeTitle('小区切换');    //更改当前页面标题
            this.main()
        },
        ready: function(){
        },
        main: function(){
            this.render(
                <Location itemMethod={bindEventLocation}/>,
                document.getElementById(name)
            )
        },
        end: function(){
            // rdata={}
            _gotonext = 'uc/edit';
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
